package com.batchap.photoframe;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.LayerManager;

import com.batchap.classes.MyMenu;

/**
 * @author QNRECK
 */
public class PhotoFrameCanvas extends Canvas {

	MainMIDlet midlet;
	// Sprite bar;
	LayerManager layermng;
	//private boolean IsActive = false;

	// -------------------------------------
	MyMenu menu;

	// -------------------------------------
	// -----------------------------------------
	/*
	 * // Player for camera private Player player; // Video control of camera
	 * private VideoControl videoControl; // Image captured by camera. private
	 * Image capturedImage; // Alert to be displayed if error occurs. private
	 * Alert alert; // String for capture locator private String captureLocator;
	 */

	// -----------------------------------------

	public PhotoFrameCanvas(final MainMIDlet midlet) {
		// super(true);
		// super(false);
		setFullScreenMode(true);
		this.midlet = midlet;
	}

	public void Init() {
		menu = new MyMenu(this);
	}

	public void ReInit() {
		this.menu.IsMenu = true;
	}

	static public void CheckMenmory() {
		Runtime runtime = Runtime.getRuntime();
		runtime.gc();
		// System.out.print("freeMemory:> " + runtime.freeMemory());
		// System.out.print(" totalMemory:> " + runtime.totalMemory());
		System.out.print(" usedMemory:> "
				+ (runtime.totalMemory() - runtime.freeMemory()));
		// System.out.println(" %freeMemory:> " + (runtime.freeMemory() * 100 /
		// runtime.totalMemory()));
		runtime = null;
	}

	private void updateCanvas() {

		if (menu.IsMenu) {
			menu.Update();
			switch (menu.Command) {
			case 1: {
				menu.IsMenu = false;
				menu.Command = 0;
				this.midlet.switchToFrameScreen("kid");
				System.out.print("Kids");
			}
				break;
			case 2: {
				menu.IsMenu = false;
				menu.Command = 0;
				this.midlet.switchToFrameScreen("flower");
				System.out.print("Flower");
			}
				break;
			case 3: {
				midlet.close();
			}
				break;
			}
		} else {
			// Show smthg to start frame
		}
	}

	private void drawCanvas(Graphics g) {

		if (menu.IsMenu)
			menu.Draw(g);

	}

	protected void pointerPressed(int x, int y) {
		System.out.println("Tap");
		if (menu.IsMenu)
			menu.ProcessTap(x, y);

		// update after Press
		updateCanvas();
	}

	protected void paint(Graphics g) {
		drawCanvas(g);
		//System.out.print("PaintCanvas");
	}

	// --------------------------------

}
